package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.*;
import com.example.exception.ServiceException;
import com.example.service.*;
import com.example.until.Result;
import com.example.until.ResultCodeEnum;
import com.example.until.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @title: Course
 * @Author wjf
 * @Date: 2022/9/29 14:34
 * @Version 1.0
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/page")
    public Result<IPage<Notice>> getAllUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(defaultValue = "") String title) {
        IPage<Notice> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.eq(StringUtils.isNotBlank(title),Notice::getTitle, title);
        IPage<Notice> page1 = noticeService.page(page, noticeLambdaQueryWrapper);
        return Result.ok(page1);
    }


    @GetMapping("/{id}")
    public Result<Notice> getCourseById(@PathVariable("id") Integer id) {
        Notice notice = noticeService.getById(id);
        return Result.ok(notice);
    }


    @PostMapping("/save")
    public Result<Boolean> saveUser(@RequestBody Notice notice) {
        notice.setAuthor(TokenUtils.getCurrentUser().getNickname());
        boolean save = noticeService.save(notice);
        return Result.ok(save);
    }

    @PostMapping("/status/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setStatus(1);
        boolean b = noticeService.updateById(notice);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/list")
    public Result getList(){
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getStatus,1);
        List<Notice> list = noticeService.list(queryWrapper);
        return Result.ok(list);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean b = noticeService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    @DeleteMapping("/delete/batch/{ids}")
    public Result<Boolean> deleteMoreUser(@PathVariable List<Integer> ids) {
        boolean b = noticeService.removeByIds(ids);
        return Result.ok(b);
    }

    @PutMapping("/update")
    public Result up(@RequestBody Notice notice){
       notice.setAuthor(TokenUtils.getCurrentUser().getNickname());
        boolean b = noticeService.updateById(notice);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

}
